package servidor.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import servidor.TCPServer;
import servidor.model.Medida;
import servidor.model.Paciente;
import servidor.model.Sensor;

public class DaoImpl implements Dao {

	private String ruta = "src/servidor/database.db";

	@Override
	public Paciente getPacienteById(int id_paciente) {
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;
		Paciente paciente = null;
		try {
			ps = c.prepareStatement("SELECT * FROM PACIENTE WHERE ID = ?");
			ps.setInt(1, id_paciente);

			ResultSet rs = ps.executeQuery();

			paciente = Paciente.createPaciente(rs.getInt(1), rs.getString(2),
					rs.getString(3), rs.getString(4), rs.getString(5),
					rs.getString(6));

			rs.close();
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return paciente;
	}

	@Override
	public Paciente getPacienteByUser(String user) {
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;
		Paciente paciente = null;
		try {
			ps = c.prepareStatement("SELECT * FROM PACIENTE WHERE USER = ?");
			ps.setString(1, user);

			ResultSet rs = ps.executeQuery();

			paciente = Paciente.createPaciente(rs.getInt(1), rs.getString(2),
					rs.getString(3), rs.getString(4), rs.getString(5),
					rs.getString(6));

			rs.close();
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return paciente;
	}

	@Override
	public List<Paciente> listaPacientes() {
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;
		Paciente paciente = null;
		List<Paciente> pacientes = new ArrayList<Paciente>();
		try {
			ps = c.prepareStatement("SELECT * FROM PACIENTE");

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				paciente = Paciente.createPaciente(rs.getInt(1),
						rs.getString(2), rs.getString(3), rs.getString(4),
						rs.getString(5), rs.getString(6));
				pacientes.add(paciente);
			}

			rs.close();
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pacientes;
	}

	@Override
	public boolean validarUser(String user) {
		List<Paciente> pacientes = new ArrayList<Paciente>();
		pacientes = listaPacientes();
		boolean enc = false;
		int cont = 0;
		while (enc == false && cont < pacientes.size()) {
			if (pacientes.get(cont).getUser().equals(user)) {
				enc = true;
				cont = pacientes.size();
			} else
				cont++;
		}
		return enc;
	}

	@Override
	public boolean validarPass(String user, String pass) {
		Paciente paciente;
		paciente = getPacienteByUser(user);
		String passBD = paciente.getPass();
		if (pass.equals(passBD))
			return true;
		else
			return false;
	}

	@Override
	public List<Sensor> listaSensores(int id_paciente) {
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;
		Sensor sensor = null;
		List<Sensor> sensores = new ArrayList<Sensor>();
		try {
			ps = c.prepareStatement("SELECT * FROM SENSOR WHERE ID_PACIENTE = ?");
			ps.setInt(1, id_paciente);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				sensor = Sensor.createSensor(rs.getInt(1), rs.getString(2),
						rs.getString(3), rs.getInt(4));
				sensores.add(sensor);
			}
			rs.close();
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return sensores;
	}

	@Override
	public List<Medida> historicoSensores(int id_sensor) {
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;
		Medida medida = null;
		List<Medida> medidas = new ArrayList<Medida>();
		try {
			ps = c.prepareStatement("SELECT * FROM MEDIDA WHERE ID_SENSOR = ?");
			ps.setInt(1, id_sensor);

			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				medida = Medida.createMedida(rs.getInt(1), rs.getString(2),
						rs.getString(3), rs.getDouble(4), rs.getString(5),
						rs.getString(6), rs.getInt(7));
				medidas.add(medida);
			}
			rs.close();
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return medidas;
	}

	@Override
	public Sensor getSensorById(String id_sensor) {
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;
		Sensor sensor = null;
		int id = Integer.parseInt(id_sensor);
		try {
			ps = c.prepareStatement("SELECT * FROM SENSOR WHERE ID = ?");
			ps.setInt(1, id);

			ResultSet rs = ps.executeQuery();

			if(rs.next()) {
				sensor = Sensor.createSensor(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4));
			}

			rs.close();
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return sensor;
	}

	@Override
	public void encenderSensor(int id_sensor) {

		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;

		try {
			ps = c.prepareStatement("UPDATE SENSOR SET ESTADO = 'ON' WHERE ID = ?");
			ps.setInt(1, id_sensor);
			ps.executeUpdate();

			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void apagarSensor(int id_sensor) {
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;

		try {
			ps = c.prepareStatement("UPDATE SENSOR SET ESTADO='OFF' WHERE ID = ?");
			ps.setInt(1, id_sensor);
			ps.executeUpdate();

			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void encenderSensorGPS(int id_paciente) {
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;

		try {
			ps = c.prepareStatement("UPDATE PACIENTE SET ESTADO_GPS = 'ON' WHERE ID = ?");
			ps.setInt(1, id_paciente);
			ps.executeUpdate();

			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void apagarSensorGPS(int id_paciente) {
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;

		try {
			ps = c.prepareStatement("UPDATE PACIENTE SET ESTADO_GPS = 'OFF' WHERE ID = ?");
			ps.setInt(1, id_paciente);
			ps.executeUpdate();

			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Medida getValact(String id_sensor) {
		// = getSensorById??
		Medida medida = null;
		int id = Integer.parseInt(id_sensor);
		List<Medida> lmedida = historicoSensores(id);
		for (int i = 0; i < lmedida.size(); i++) {
			if (i == lmedida.size() - 1)
				medida = lmedida.get(i);
		}
		return medida;
	}

	@Override
	public boolean estadoSensor(String id_sensor) {
		Sensor sensor = getSensorById(id_sensor);
		String estado = sensor.getEstado();		

		if (estado.equals("ON"))
			return true;
		else
			return false;
	}

	@Override
	public boolean verificarSensorPaciente(String id_sensor, int id_paciente)  {
		
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;
		int id = Integer.parseInt(id_sensor);
		int cont = 0;
		try {
			ps = c.prepareStatement("SELECT * FROM SENSOR WHERE ID = ? AND ID_PACIENTE = ?");
			ps.setInt(1, id);
			ps.setInt(2, id_paciente);

			ResultSet rs = ps.executeQuery();
			
			if(rs.next()){
				cont++;
			}
			
			rs.close();
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		if(cont==0) 
			return false;
		else
			return true;
	
				
		
	}

	@Override
	public void borrarUsuario(String user) {
		// DELETE FROM USUARIO WHERE NOMBRE = ?
		
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;
		try {
			ps = c.prepareStatement("DELETE FROM PACIENTE WHERE USER = ?");
			ps.setString(1, user);

			ps.executeUpdate();
			
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void modificarPaciente(String user, String pass) {
		
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;
		try {
			ps = c.prepareStatement("UPDATE PACIENTE WHERE USER = ? AND PASS = ?");
			ps.setString(1, user);
			ps.setString(2, pass);

			ps.executeUpdate();
			
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void insertarUsuario(String user, String pass) {
		
		Connection c = TCPServer.conectarA(ruta);
		PreparedStatement ps = null;
		try {
			ps = c.prepareStatement("INSERT INTO PACIENTE (USER, PASS) VALUES (?,?)");
			ps.setString(1, user);
			ps.setString(2, pass);

			ps.executeUpdate();
			
			c.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

}
